<template>
  <component
    :is="tag"
    :class="buttonClasses"
    :disabled="disabled || loading"
    v-bind="$attrs"
    @click="handleClick"
  >
    <!-- 加载状态 -->
    <div v-if="loading" class="btn-loading">
      <LoadingSpinner class="w-4 h-4" />
      <span v-if="loadingText" class="ml-2">{{ loadingText }}</span>
    </div>
    
    <!-- 正常状态 -->
    <template v-else>
      <!-- 前置图标 -->
      <component 
        v-if="iconLeft" 
        :is="iconLeft" 
        :class="iconClasses"
      />
      
      <!-- 按钮文字 -->
      <span v-if="$slots.default" :class="{ 'ml-2': iconLeft, 'mr-2': iconRight }">
        <slot />
      </span>
      
      <!-- 后置图标 -->
      <component 
        v-if="iconRight" 
        :is="iconRight" 
        :class="iconClasses"
      />
    </template>
  </component>
</template>

<script setup lang="ts">
import { computed, type Component } from 'vue'
import LoadingSpinner from './LoadingSpinner.vue'

export interface ButtonProps {
  // 基础属性
  variant?: 'primary' | 'secondary' | 'outline' | 'ghost' | 'link' | 'danger'
  size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'
  tag?: string
  
  // 状态
  disabled?: boolean
  loading?: boolean
  loadingText?: string
  
  // 图标
  iconLeft?: Component
  iconRight?: Component
  
  // 样式
  rounded?: boolean
  shadow?: boolean
  block?: boolean
}

const props = withDefaults(defineProps<ButtonProps>(), {
  variant: 'primary',
  size: 'md',
  tag: 'button',
  disabled: false,
  loading: false,
  rounded: false,
  shadow: true,
  block: false,
})

const emit = defineEmits<{
  click: [event: Event]
}>()

// 基础样式类
const baseClasses = 'inline-flex items-center justify-center font-medium transition-all duration-300 focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed'

// 变体样式
const variantClasses = computed(() => {
  const variants = {
    primary: 'bg-primary-500 text-white hover:bg-primary-600 focus:ring-primary-500 shadow-md hover:shadow-lg active:transform active:scale-[0.98]',
    secondary: 'bg-gray-100 dark:bg-gray-700 text-gray-900 dark:text-gray-100 hover:bg-gray-200 dark:hover:bg-gray-600 focus:ring-gray-500 border border-gray-300 dark:border-gray-600',
    outline: 'border-2 border-primary-500 text-primary-500 hover:bg-primary-500 hover:text-white focus:ring-primary-500 bg-transparent',
    ghost: 'text-primary-500 hover:bg-primary-50 dark:hover:bg-primary-900/20 focus:ring-primary-500 bg-transparent',
    link: 'text-primary-500 hover:text-primary-600 focus:ring-primary-500 bg-transparent underline-offset-4 hover:underline',
    danger: 'bg-red-500 text-white hover:bg-red-600 focus:ring-red-500 shadow-md hover:shadow-lg active:transform active:scale-[0.98]'
  }
  return variants[props.variant]
})

// 尺寸样式
const sizeClasses = computed(() => {
  const sizes = {
    xs: 'px-2 py-1 text-xs',
    sm: 'px-3 py-1.5 text-sm',
    md: 'px-4 py-2 text-sm',
    lg: 'px-6 py-2.5 text-base',
    xl: 'px-8 py-3 text-lg'
  }
  return sizes[props.size]
})

// 圆角样式
const roundedClasses = computed(() => {
  if (props.rounded) {
    const roundedSizes = {
      xs: 'rounded-full',
      sm: 'rounded-full',
      md: 'rounded-full',
      lg: 'rounded-full',
      xl: 'rounded-full'
    }
    return roundedSizes[props.size]
  }
  return 'rounded-md'
})

// 阴影样式
const shadowClasses = computed(() => {
  if (!props.shadow) return ''
  
  const shadowTypes = {
    primary: 'shadow-md hover:shadow-lg',
    secondary: 'shadow-sm hover:shadow-md',
    outline: 'hover:shadow-md',
    ghost: '',
    link: '',
    danger: 'shadow-md hover:shadow-lg'
  }
  return shadowTypes[props.variant]
})

// 图标样式
const iconClasses = computed(() => {
  const iconSizes = {
    xs: 'w-3 h-3',
    sm: 'w-4 h-4',
    md: 'w-4 h-4',
    lg: 'w-5 h-5',
    xl: 'w-6 h-6'
  }
  return iconSizes[props.size]
})

// 组合所有样式类
const buttonClasses = computed(() => [
  baseClasses,
  variantClasses.value,
  sizeClasses.value,
  roundedClasses.value,
  shadowClasses.value,
  {
    'w-full': props.block,
    'cursor-not-allowed': props.disabled || props.loading
  }
])

// 点击处理
const handleClick = (event: Event) => {
  if (!props.disabled && !props.loading) {
    emit('click', event)
  }
}
</script>

<style scoped>
.btn-loading {
  @apply flex items-center justify-center;
}

/* 加载动画增强 */
.btn-loading .loading-spinner {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
